//
//  MarketViewController.m
//  BTCoin
//
//  Created by Robin on 13/03/2018.
//  Copyright © 2018 Robin. All rights reserved.
//

#import "MarketViewController.h"
#import "MarketHeadTitleView.h"
#import "BTKMarketView.h"
#import "MarketService.h"
#import "MarketHomeListModel.h"
#import <MJExtension.h>
#import "Y_StockChartViewController.h"
#import "MarkDeatilViewController.h"

@interface MarketViewController ()<UIScrollViewDelegate,BTMarketViewDelegate>
@property (nonatomic,strong) UIScrollView * scrollView;
@property (nonatomic,strong) MarketHeadTitleView * headContainerView;
@property (nonatomic,strong) BTKMarketView * btkMarketView;
@property (nonatomic, strong) NSMutableArray    *listSourceArr; //数据源
@end

@implementation MarketViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setLineHidden:YES];
    [self initUIHead];
}

- (void)initUIHead {
    @WeakObj(self)

    self.headContainerView = [[MarketHeadTitleView alloc] init];
    self.headContainerView.backgroundColor = [UIColor clearColor];
    [self.headView addSubview:self.headContainerView];
    
    self.headContainerView.block = ^(NSInteger returnValue) {
        selfWeak.scrollView.contentOffset = CGPointMake(kScreenWidth*returnValue, 0);
    };
    
    self.scrollView = [[UIScrollView alloc] init];
    self.scrollView.delegate = self;
    self.scrollView.pagingEnabled = YES;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:self.scrollView];
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.headView.mas_bottom);
        make.leading.bottom.trailing.equalTo(self.view);
    }];
    
    self.txtTitle.text = @"行情";
    self.btkMarketView = [[BTKMarketView alloc] init];
    self.btkMarketView.delegate = self;
    [self.scrollView addSubview:self.btkMarketView];
    __weak typeof(self) weakSelf = self;
    self.btkMarketView.block = ^(NSInteger returnValue) {
        [weakSelf.listSourceArr removeAllObjects];
        if (returnValue == 0) {
            [weakSelf sendRequestWithcoinType:[NSString stringWithFormat:@"%ld",(long)USDT_Type + 1] AndTradeType:@"0"];
        }else if (returnValue == 1) {
            [weakSelf sendRequestWithcoinType:[NSString stringWithFormat:@"%ld",(long)BTC_Type + 1] AndTradeType:@"0"];
        }else if (returnValue == 2){
            [weakSelf sendRequestWithcoinType:[NSString stringWithFormat:@"%ld",(long)ETH_Type + 1] AndTradeType:@"0"];
        }
    };
    [self.btkMarketView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.bottom.equalTo(self.scrollView);
        make.width.mas_equalTo(kScreenWidth);
        make.height.equalTo(self.scrollView);
    }];
    [self sendRequestWithcoinType:@"1" AndTradeType:@"0"];
}

#pragma mark - event actions
- (void)sendRequestWithcoinType:(NSString *)coinType AndTradeType:(NSString *)tradeType {
    NSDictionary *dic = @{@"coinType":coinType,
                          @"tradeType":tradeType
                          };
    Loading_Pig_Show(self.view);
    [[MarketService sharedMarketService] requestPostMarketHomeList:dic success:^(id responseObject) {
        Loading_Hide(self.view);
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            MarketHomeListModel * userInfo = [[MarketHomeListModel alloc] initWithDictionary:responseObject error:nil];
            
            if (SuccessCode == userInfo.code) {
                NSDictionary *dic = [responseObject objectForKey:@"ftrademappings"];
                NSArray *arr = [dic objectForKey:@"fcurrencyInfos"];
                NSMutableArray  *tempArr = [[NSMutableArray alloc] init];
                for (NSDictionary *tempDic in arr) {
                    MarketHomeListContentModel *model = [[MarketHomeListContentModel alloc] init];
                    [model mj_setKeyValues:tempDic];
                    [tempArr addObject:model];
                }
                
                [self.btkMarketView reloadTableviewWithSourceArr:tempArr];
            } else {
                [PopupViewHUD showErrorPopup:userInfo.msg];
            }
           
        }
    } fail:^(NSError *error) {
        Loading_Hide(self.view);
        
    }];
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    double index_ = scrollView.contentOffset.x/kScreenWidth;
    [self.headContainerView updateLinePoint:(NSInteger)(index_+0.5)];
}

#pragma mark - markViewDelegate
- (void)JumpToMarketListCellActionWithType:(NSString *)type {
    [self.navigationController pushViewController:[MarkDeatilViewController new] animated:YES];
}

@end
